﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

public partial class SummaryInfo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check if file exist
        string filePath = Server.MapPath("excel\\Month\\干部安全责任制考评汇总表.xls");
        FileInfo ExcelFile = new FileInfo(filePath);
        if (ExcelFile.Exists)
        {
            HyperLink excelLink = new HyperLink();
            excelLink.Text = "干部安全责任制考评汇总表";
            excelLink.NavigateUrl = "~/excel/month/干部安全责任制考评汇总表.xls";
            PlaceHolder_Excel.Controls.Add(excelLink);
        }
        else
        {
            Label nofileInfo = new Label();
            nofileInfo.Text = "本月的汇总表还没有生成！";
            PlaceHolder_Excel.Controls.Add(nofileInfo);
            
        }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Generate header, delete the former file and copy layout to the destination
        string targetfilepath = Server.MapPath(@"excel\Month\干部安全责任制考评汇总表.xls");

        FileInfo targetFile = new FileInfo(targetfilepath);
        if( targetFile.Exists )
        {
            targetFile.Delete();
            targetFile = null;
        }

        string layoutfilepath = Server.MapPath(@"excel\layout\layout.xls");
        FileInfo layoutFile = new FileInfo(layoutfilepath);
        if(layoutFile.Exists)
        {
            targetFile  = layoutFile.CopyTo(targetfilepath);
            if(targetFile.Exists)
            {
                //Read data from data base
                StaffDB db = new StaffDB();
                DataSet ds = db.GetStaffSummary();
                DataTable dt = ds.Tables["staffSum"];
                //Write data
                Excel.Application oExcel = new Excel.Application();
                try
                {
                    Excel.Workbook layoutExcel = oExcel.Workbooks.Open(
                        targetfilepath,
                        0,
                        false,
                        5,
                        "",
                        "",
                        false,
                        Excel.XlPlatform.xlWindows,
                        "",
                        true,
                        false,
                        0,
                        true,
                        false,
                        false);

                    Excel.Worksheet layoutExcelSheet = (Excel.Worksheet)layoutExcel.Sheets[1];
                    int year = DateTime.Today.Year;
                    int month = DateTime.Today.Month;
                    string header = "自贡工务段 " + year.ToString() + " 年 " + month.ToString() + " 月管理人员安全责任制考核汇总表";
                    layoutExcelSheet.Cells[1,1] = header;
                    int curExcelRowIdx = 5;
                    //Get all rewardBonus
                    RewardDB rdb = new RewardDB();
                    RewardDetail[] rewards = rdb.GetRewards();
                    
                    foreach(DataRow dr in dt.Rows)
                    {
                        int j = 1; // for data side
                        decimal score = 0;
                        int EvalLevel = 0;
                        int RewardLevel = 0;
                        int curWithdraw = 0;
                        for(int i=1; i<= 18; i++)
                        {
                            
                            layoutExcelSheet.Cells[curExcelRowIdx, i] = dr[j-1].ToString();
                            if( i == 14 )
                            {
                                //compute the level of evaluation
                                score = (decimal)dr[i - 1];
                                string level = "失格";
                                if(score >= 90)
                                {
                                    EvalLevel = 1;
                                    level = "优秀";
                                }
                                else if( score >= 80)
                                {
                                    EvalLevel = 2;
                                    level = "良好";
                                }
                                else if(score >= 60)
                                {
                                    EvalLevel = 3;
                                    level = "良好";
                                }
                                
                                i++;
                                layoutExcelSheet.Cells[curExcelRowIdx, i] = level;
                            }
                            if( i == 15 )
                            {
                                RewardLevel = (int)dr[i - 1];
                            }
                            if( i == 16)
                            {
                                curWithdraw = (int)dr[i - 1];
                            }
                            j++;
                        }
                        // the last column
                        //Get the bonus
                        uint curBonus = 0;
                        foreach(RewardDetail rd in rewards)
                        {
                            if(RewardLevel == rd.Level)
                            {
                                switch (EvalLevel)
                                {
                                    case 1:
                                        curBonus = rd.EvalHigh;
                                        break;
                                    case 2:
                                        curBonus = rd.EvalMiddle;
                                        break;
                                    case 3:
                                        curBonus = rd.EvalLow;
                                        break;
                                    default:
                                        curBonus = 0;
                                        break;
                                }

                                break;
                            }
                        }
                        curBonus -= (uint)curWithdraw;
                        layoutExcelSheet.Cells[curExcelRowIdx, 19] = curBonus;
                        curExcelRowIdx++;
                    }
                   
                    layoutExcel.Save();

                    oExcel.Workbooks.Close();

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oExcel.Quit();
                    oExcel = null;
                }
            }
        }
       
 
    }
}
